package com.example.front.mapper;

import com.example.common.dto.Pagination;
import com.example.front.dto.BoxSearchDto;
import com.example.front.dto.ButtonDto;
import com.example.front.dto.ButtonSearchDto;
import com.example.front.entity.Box;
import com.example.front.entity.Button;
import com.example.front.entity.Event;
import com.example.passport.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ButtonMapper {


    @Select("<script>" +
            "SELECT COUNT(*) FROM `button` WHERE" +
            " `tenant_id`=#{button.tenantId}" +
            " AND `key` = #{button.key}" +
            " AND `category_id` = #{button.categoryId}" +
            "<if test='button.id!=null'> AND `id`!=#{button.id}</if> AND `status`!=" + User.STATUS_DELETE +
            "</script>")
    int findButtonByKey(@Param("button") Button button);

    @Select("SELECT * FROM `button` WHERE `tenant_id`=#{button.tenantId} AND `id`=#{button.id} AND `status`!=" + User.STATUS_DELETE)
    Button findById(@Param("button") Button button);

    @Select("<script>" +
            "SELECT COUNT(*) FROM `button` WHERE `tenant_id`=#{button.tenantId}" +
            " AND `event_id` = #{button.eventId}" +
            " AND `category_id` = #{button.categoryId}" +
            "<if test='button.id!=null'> AND `id`!=#{button.id}</if> AND `status`!=" + User.STATUS_DELETE +
            "</script>")
    int findButtonByEvent(@Param("button") Button button);

    @Insert("INSERT INTO `button` (`tenant_id`, `key`, `event_id`, `category_id`)" +
            " VALUES (#{button.tenantId}, #{button.key}, #{button.eventId},#{button.categoryId})")
    int create(@Param("button") Button button);

    @Update("UPDATE `button` SET `status` ="+ User.STATUS_DELETE+
            " WHERE `id`=#{button.id} AND `tenant_id`=#{button.tenantId} AND `status`!=" + User.STATUS_DELETE)
    int delete(@Param("button") Button button);


    @Update("UPDATE `button` SET `status` ="+ User.STATUS_DELETE+
            " WHERE `category_id`=#{button.categoryId} AND `tenant_id`=#{button.tenantId} AND `status`!=" + User.STATUS_DELETE)
    void deleteByCategoryId(@Param("button") Button button);

    @Select("<script>" +
            "SELECT * FROM `button`" +
            "<where>" +
            "<if test='condition.id!=0'> AND `id`=#{condition.id}</if>" +
            "<if test='condition.key!=0'> AND `key`=#{condition.key} </if>" +
            "<if test='condition.categoryId!=null'> AND `category_id`=#{condition.categoryId} </if>" +
            "<if test='condition.eventId!=null'> AND `event_id`=#{condition.eventId} </if>" +
            " AND `tenant_id`=#{condition.tenantId} " +
            "</where> AND `status`!=" + User.STATUS_DELETE +
            " ORDER BY `id` DESC LIMIT #{pagination.offset}, #{pagination.limit}" +
            "</script>")
    List<Button> findAll(@Param("condition") ButtonSearchDto condition, @Param("pagination") Pagination pagination);

    @Select("<script>" +
            "SELECT count(*) FROM `button`" +
            "<where>" +
            "<if test='condition.id!=0'> AND `id`=#{condition.id}</if>" +
            "<if test='condition.key!=0'> AND `key`=#{condition.key} </if>" +
            "<if test='condition.categoryId!=null'> AND `category_id`=#{condition.categoryId} </if>" +
            "<if test='condition.eventId!=null'> AND `event_id`=#{condition.eventId} </if>" +
            " AND `tenant_id`=#{condition.tenantId} " +
            "</where> AND `status`!=" + User.STATUS_DELETE +
            "</script>")
    long count(@Param("condition") ButtonSearchDto condition);

    @Update("UPDATE `button` SET " +
            "`key` = #{button.key} " +
            ", `category_id` = #{button.categoryId}" +
            ", `event_id` = #{button.eventId}" +
            " WHERE `id`=#{button.id} AND `tenant_id`=#{button.tenantId}  AND `status`!=" + User.STATUS_DELETE)
    int update(@Param("button") Button button);

    @Select("SELECT * FROM `button` WHERE `tenant_id`=#{button.tenantId} " +
            "AND `category_id` = #{button.categoryId} " +
            "AND `key` = #{button.key} AND `status`!=" + User.STATUS_DELETE+" LIMIT 1")
    ButtonDto event(@Param("button") Button button);

    @Select("SELECT count(*) FROM `button` WHERE `tenant_id` = #{tenantId} AND `event_id` = #{eventId} AND `status`!=" + User.STATUS_DELETE)
    long findByEventId(@Param("tenantId") long tenantId, @Param("eventId") long eventId );
}
